package com.youkeyi.ddy.cloud.application.applet.crm.type.controller;

import com.youkeyi.ddy.cloud.common.api.application.applet.crm.type.AppletCustomerTypeCloudService;
import com.youkeyi.ddy.cloud.common.api.application.applet.crm.type.dto.AppletCustomerTypeListCloudDto;
import com.youkeyi.ddy.cloud.common.api.application.applet.crm.type.dto.AppletCustomerTypeListItemCloudDto;
import com.youkeyi.ddy.cloud.common.api.application.applet.crm.type.param.AppletCustomerTypeListCloudParam;
import com.youkeyi.ddy.cloud.common.api.basic.crm.type.CrmCustomerTypeCloudService;
import com.youkeyi.ddy.cloud.common.api.basic.crm.type.dto.CrmCustomerTypeListCloudDto;
import com.youkeyi.ddy.cloud.common.api.basic.crm.type.param.CrmCustomerTypeQueryCloudParam;
import com.youkeyi.ddy.cloud.common.context.YoukeyiSystemContext;
import com.youkeyi.ddy.cloud.common.enums.ResponseCodeEnum;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * AppletCustomerTypeController
 *
 * @Author liuxiawang
 * @Date 2024/4/11 4:09 PM
 * @Location shenzhen.china
 */
@Service
public class AppletCustomerTypeController implements AppletCustomerTypeCloudService {
    
    @Autowired
    private CrmCustomerTypeCloudService crmCustomerTypeCloudService;

    @Override
    public AppletCustomerTypeListCloudDto list(AppletCustomerTypeListCloudParam param) {
        AppletCustomerTypeListCloudDto dto = new AppletCustomerTypeListCloudDto();
        String corpKey = YoukeyiSystemContext.getCorpPrimaryKey();
        if (StringUtils.isBlank(corpKey)) {
            dto.setErrorMessage(ResponseCodeEnum.TOKEN_INVALID.getMessage());
            return dto;
        }
        if (null == param) {
            dto.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return dto;
        }

        CrmCustomerTypeQueryCloudParam customerTypeQueryCloudParam = new CrmCustomerTypeQueryCloudParam();
        customerTypeQueryCloudParam.setCustomerTypePrimaryKeys(param.getCustomerTypePrimaryKeys());
        customerTypeQueryCloudParam.setCorpKeys(Collections.singletonList(corpKey));
        customerTypeQueryCloudParam.setCodes(param.getCodes());
        customerTypeQueryCloudParam.setCodeFuzzy(param.getCodeFuzzy());
        customerTypeQueryCloudParam.setNames(param.getNames());
        customerTypeQueryCloudParam.setNameFuzzy(param.getNameFuzzy());
        customerTypeQueryCloudParam.setLevel(param.getLevel());
        customerTypeQueryCloudParam.setParentKeys(param.getParentKeys());
        customerTypeQueryCloudParam.setPageSize(param.getPageSize());
        customerTypeQueryCloudParam.setCurrentPage(param.getCurrentPage());
        customerTypeQueryCloudParam.setNeedParent(Boolean.TRUE);
        CrmCustomerTypeListCloudDto customerTypeDtos = crmCustomerTypeCloudService.query(customerTypeQueryCloudParam);
        if (null != customerTypeDtos && CollectionUtils.isNotEmpty(customerTypeDtos.getRows())) {
            List<AppletCustomerTypeListItemCloudDto> items = new ArrayList<>();
            customerTypeDtos.getRows().forEach(customerType -> {
                AppletCustomerTypeListItemCloudDto item = new AppletCustomerTypeListItemCloudDto();
                item.setCustomerTypePrimaryKey(customerType.getCustomerTypePrimaryKey());
                item.setName(customerType.getName());
                item.setCode(customerType.getCode());
                item.setLevel(customerType.getLevel());
                item.setParentKey(customerType.getParentKey());
                item.setParentName(customerType.getParentName());
                item.setDesc(customerType.getDesc());
                item.setIsDeleted(customerType.getIsDeleted());
                item.setIsDeletedName(customerType.getIsDeletedName());
                item.setCreateUserKey(customerType.getCreateUserKey());
                item.setCreateTime(customerType.getCreateTime());
                item.setUpdateUserKey(customerType.getUpdateUserKey());
                item.setUpdateTime(customerType.getUpdateTime());
                item.setCreateUserName(customerType.getCreateUserName());
                item.setUpdateUserName(customerType.getUpdateUserName());
                items.add(item);
            });
            dto.setRows(items);
            dto.setTotal(customerTypeDtos.getTotal());
        }
        dto.setSuccessMessage(ResponseCodeEnum.SUCCESS.getMessage());
        return dto;
    }

}
